package com.ybvtc.controller;

import com.ybvtc.domain.User;
import com.ybvtc.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    public String login(User inputUser, HttpSession session) {

        System.out.println(inputUser);
        User user = userService.login(inputUser);
        if (user != null) {
            System.out.println("登录成功");
            session.setAttribute("user",user);
            return "redirect:/main";
        } else {
            System.out.println("登录失败");
            return null;
        }

    }

    @RequestMapping("/main")
    public String toMain() {
        return "main";
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "login";
    }


}
